package com.example.userservice.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.userservice.mapper.UserMapper;
import com.example.userservice.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserMapper userMapper;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(HttpServletRequest request){
        String userEmail = request.getParameter("userEmail");
        String password = request.getParameter("userPassword");

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("userEmail",userEmail);
        wrapper.eq("userPassword",password);
        User user = userMapper.selectOne(wrapper);
        if (user == null){
            return null;
        }else{
            System.out.println(user);
            user.setUserPassword(null);
            return JSON.toJSONString(user);
        }
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String register(HttpServletRequest request){ // 用户注册 需要传入一个User实体
       JSONObject JSONUser = JSONObject.parseObject(request.getParameter("User"));
       User user = JSONObject.toJavaObject(JSONUser,User.class);

       Map<String, Object> map = new HashMap<>();
       map.put("userEmail", user.getUserEmail());
       if(!userMapper.selectByMap(map).isEmpty())
           return "-1"; // 邮箱已注册

       user.setUserGmtCreate(null);
       user.setUserGmtUpdate(null);

       userMapper.insert(user);
       QueryWrapper<User> wrapper = new QueryWrapper<>();
       wrapper.eq("userEmail",user.getUserEmail());
       return JSON.toJSONString(userMapper.selectOne(wrapper));
    }

    @RequestMapping(value = "/changePasswordByEmail",method = RequestMethod.POST)
    public String changPasswordByEmail(HttpServletRequest request) { // 修改用户密码 需要提供正确的用户实体，起码包括修改后密码和id 调用前请先通过邮箱验证！
        JSONObject JSONUser = JSONObject.parseObject(request.getParameter("User"));
        User user = JSONObject.toJavaObject(JSONUser, User.class);

        if(userMapper.updateById(user) == 0)
            return "0";    // 修改错误
        else
            return JSON.toJSONString(userMapper.selectById(user.getUserId())); // 返回修改后用户实体
    }

    @RequestMapping(value = "/changePasswordByPassword",method = RequestMethod.POST)
    public String changPasswordByPassword(HttpServletRequest request) { // 修改用户密码 需要提供正确的用户实体，起码包括原密码和id，额外需要包括修改后密码
        JSONObject JSONUser = JSONObject.parseObject(request.getParameter("User"));
        String changedPassword = request.getParameter("changedPassword");
        User user = JSONObject.toJavaObject(JSONUser, User.class);


        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",user.getUserId());
        wrapper.eq("userPassword",user.getUserPassword());
        if(userMapper.selectOne(wrapper) == null)
            return "0"; // id和密码对不上
        else{
            user.setUserPassword(changedPassword);
            if(userMapper.updateById(user) == 0)
                return "-1"; // 更新失败
            else
                return JSON.toJSONString(userMapper.selectById(user.getUserId())); // 返回修改后用户实体
        }
    }

    @RequestMapping("/selectUserByEmail/{EmailToUser}")
    public boolean selectUserByEmail(@PathVariable("EmailToUser") String emailToUser){
        Map<String, Object> map = new HashMap<>();
        map.put("userEmail", emailToUser);
        return userMapper.selectByMap(map).isEmpty();
    }

    @RequestMapping("/hello")
    String test(){
        return "hello!!!";
    }
}
